layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * 众测与活动管理
     */
    var Activity = {
        tableId: "activityTable"
    };

    /**
     * 初始化表格的列
     */
    Activity.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'type', sort: true,hide: true, title: '项目类型'},
            {field: 'content',hide: true,  sort: true, title: '内容'},
            {field: 'title', sort: true, title: '活动名称'},
            {field: 'watch', sort: true, title: '浏览量'},
            {field: 'workCount', sort: true, title: '投稿数'},
            {field: 'commentCount', sort: true, title: '评论数'},
            {field: 'markName', sort: true, title: '标识'},
            {field: 'activityStatus', sort: true, title: '状态'},
            {field: 'adminName', sort: true, title: '发布人'},
            {field: 'rewardWay', sort: true,hide: true,  title: '奖励方式'},
            {field: 'applyWay', sort: true,hide: true,  title: '申请方式'},
            {field: 'maxAttendCount',hide: true,  sort: true, title: '最大参与人数'},
            {field: 'maxApplicantsCount', hide: true, sort: true, title: '最大报名人数'},
            {field: 'alreadyApplicationsCount', hide: true, sort: true, title: '已报名人数'},
            {field: 'alreadyApplicationsList',hide: true,  sort: true, title: '已报名单（id用逗号隔开）'},
            {field: 'finalAttendCount', sort: true,hide: true,  title: '最终参与人数'},
            {field: 'finalAttendList', hide: true, sort: true, title: '最终参与人名单(id用逗号隔开)'},
            {field: 'submissList', hide: true, sort: true, title: '投稿名单（id用逗号隔开）'},
            {field: 'rewardList', hide: true, sort: true, title: '奖励名单（id用逗号隔开）'},
            {field: 'swParticipationWay', hide: true, sort: true, title: '申请方式的开关'},
            {field: 'swRewardList', hide: true, sort: true, title: '奖励名单的开关'},
            {field: 'swReport', hide: true, sort: true, title: '测评报告的开关'},
            {field: 'praise', sort: true,hide: true, title: '点赞数'},
            {field: 'swTestList',hide: true,  sort: true, title: '试用名单的开关'},
            {field: 'beginApplyTime',hide: true,  sort: true, title: '开始报名时间'},
            {field: 'endApplyTime',hide: true,  sort: true, title: '结束报名时间'},
            {field: 'beginSubmissionTime',hide: true,  sort: true, title: '开始投稿时间'},
            {field: 'endSubmissionTime',hide: true,  sort: true, title: '结束投稿时间'},
            {field: 'activityEndTime',hide: true,  sort: true, title: '活动结束时间'},
            {field: 'deptName', sort: true,hide: true, title: '部门'},
            {field: 'createTime', sort: true, title: '新增时间'},
            {field: 'updateTime', hide: true, sort: true, title: '修改时间' },
            {field: 'recordStatus',hide: true,  sort: true, title: '记录状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Activity.search = function () {
        var queryData = {};
        queryData['status'] = $("#status").val();
        queryData['title'] = $("#title").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['goodsNo'] = $("#goodsNo").val();
        queryData['id'] = $("#id").val();
        var uploadTime = $("#uploadTime").val();
        if(uploadTime !=""){
            var split = uploadTime.split("-");
            queryData["createTimeMin"]=split[0]
            queryData["createTimeMax"]=split[1]
        }
        table.reload(Activity.tableId, {where: queryData
            ,page:{
                curr:1
            }
        });

    };

    /**
     * 弹出添加对话框
     */
    Activity.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加活动',
            area:["70%","70%"],
            maxmin:true,
            content: Feng.ctxPath + '/activity/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Activity.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Activity.exportExcel = function () {
        var checkRows = table.checkStatus(Activity.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Activity.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑活动',
            area:["70%","70%"],
            maxmin:true,
            content: Feng.ctxPath + '/activity/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Activity.tableId);
            }
        });
    };
    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Activity.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/activity/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Activity.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Activity.tableId,
        url: Feng.ctxPath + '/activity/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Activity.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Activity.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Activity.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Activity.exportExcel();
    });

    laydate.render({
        elem: '#uploadTime',
        type: 'datetime',
        range:true,

    });


    // 工具条点击事件
    table.on('tool(' + Activity.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Activity.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Activity.onDeleteItem(data);
        }
    });
});
